package com.chuangyang.fixboxmaster.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.chuangyang.fixboxlib.widgets.PullDownRefreshListView;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.NewOrder;
import com.chuangyang.fixboxmaster.service.FixBoxApi;
import com.chuangyang.fixboxmaster.service.GsonRequest;
import com.chuangyang.fixboxmaster.ui.ModuleActivity;
import com.chuangyang.fixboxmaster.ui.adapter.OrderListAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/**
 * Created by vanshu on 15/4/9 11:14.
 */
public class OrderListFragment extends BaseFragment implements AdapterView.OnItemClickListener, PullDownRefreshListView.OnRefreshListener {

    private static final String TAG = OrderListFragment.class.getSimpleName();
    private View mContentView;
    public static final String ORDER_TYPE = "order_type";
    public static final int ORDER_UNFINISHED = 2;
    public static final int ORDER_HISTORY = 1;
    private OrderListAdapter mAdapter;
    private ArrayList<NewOrder> ordersList = new ArrayList<>();
    private PullDownRefreshListView mListView;
    private OnPullRefreshListener listener;
    public int mCurrentType;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mCurrentType = getArguments().getInt(ORDER_TYPE, -1);
        mContentView = inflater.inflate(R.layout.repair_list_view, container, false);
        mListView = (PullDownRefreshListView) mContentView.findViewById(R.id.listview);
        mAdapter = new OrderListAdapter(ordersList);
        mListView.setAdapter(mAdapter);
        mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
        mListView.setOnRefreshListener(this);
        mListView.setOnItemClickListener(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setContentView(mContentView);
        loadData();
    }

    public boolean canCollectionViewScrollUp() {
        return ViewCompat.canScrollVertically(mListView, -1);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent orderDetailIntent = new Intent(getActivity(), ModuleActivity.class);
        if (ordersList.get(position).appType.equals("1000")) { //1000表示企业订单
            orderDetailIntent.putExtra(ModuleActivity.TYPE, ModuleActivity.B2B_ORDER_DETAL_ACTIVITY);
            orderDetailIntent.putExtra(ModuleActivity.ORDER_ID, String.valueOf(ordersList.get(position).id));
        } else  {
            orderDetailIntent.putExtra(ModuleActivity.TYPE, ModuleActivity.ORDER_DETAL_ACTIVITY);
            orderDetailIntent.putExtra(ModuleActivity.ORDER_ID, String.valueOf(ordersList.get(position).id));
        }
        startActivity(orderDetailIntent);
    }

    public ListView getListView() {
        return mListView;
    }

    @Override
    public void onRefresh() {
        loadData();
    }


    public void updateListView(OnPullRefreshListener listener) {
        this.listener = listener;
        ordersList.clear();
        mListView.setAdapter(mAdapter);
        loadDataWithoutFootLoading();
    }


    public void loadData() {
        mListView.setLoading();
        loadDataWithoutFootLoading();
    }

    public void loadDataWithoutFootLoading() {
        HashMap<String, String> param = new HashMap<>();
        param.put("pageIndex", String.valueOf(ordersList.size()));
        if (mCurrentType == OrderListFragment.ORDER_UNFINISHED) {
            param.put("type", String.valueOf(mCurrentType));
        } else if (mCurrentType == OrderListFragment.ORDER_HISTORY) {
            param.put("type", String.valueOf(mCurrentType));
        }
        addRequest(new GsonRequest<>(FixBoxApi.GET_ALL_MY_ORDER, param, NewOrder.NewOrderInfo.class, responseListener));
    }

    private Response.Listener<NewOrder.NewOrderInfo> responseListener = new Response.Listener<NewOrder.NewOrderInfo>() {
        @Override
        public void onResponse(NewOrder.NewOrderInfo response) {
            ordersList.addAll(Arrays.asList(response.result.orderList));
            mAdapter.setCurrentType(mCurrentType);
            mAdapter.notifyDataSetChanged();
            showResult();
            if (ordersList.size() >= response.result.totalCount)
                mListView.setLoadingEnd();
            else
                mListView.setLoadingIdle();
            if (listener != null) {
                listener.onPullRefresh();
            }
        }
    };
}
